#include "ImaConf.h"
#include "ImaGlobals.h"
#include "ImaConstants.h"
#include <string.h>

ImaConf::ImaConf(char *pConfFile) {
	char log_msg[1024];
	sprintf(log_msg, "ImaConf::ImaConf called with %s", (pConfFile != NULL) ? pConfFile : "NULL parameter");
	LOG(debug, __FILE__, __LINE__, log_msg); 

	if(pConfFile != NULL) {
		strcpy(mConfFile, pConfFile);
	} else {
		//TODO: throw exception
	}
}


int ImaConf::parseConfFile() {
	LOG(debug, __FILE__, __LINE__, "ImaConf::parseConfFile called");

	json_t* pRoot = NULL;
	json_error_t error;
	pRoot = json_load_file(mConfFile, 0, &error);
	if(!pRoot) {
		char log_msg[1024];
		sprintf(log_msg, "Failed to parse json conf file %s\n; error: %s\n", mConfFile, error.text);
		LOG(debug, __FILE__, __LINE__, log_msg);
		
		return -1;
	}	

	json_t* pPollingIntervalObj = json_object_get(pRoot, IMA_JSON_KEY_POLLING_INTERVAL);
	if(pPollingIntervalObj == NULL) {
		char log_msg[1024];
		sprintf(log_msg, "Conf file %s doesn't contain the polling interval; setting the polling interval to default %lu minutes", mConfFile, IMA_POLLING_INTERVAL);
		LOG(info, __FILE__, __LINE__, log_msg);
		mPollingInterval = IMA_POLLING_INTERVAL;
	} else {
		if(json_is_integer(pPollingIntervalObj)) {
			mPollingInterval = json_integer_value(pPollingIntervalObj);
		} else {
			LOG(error, __FILE__, __LINE__, "Polling interval should be an integer object");
			json_decref(pRoot);
			return -1;
		}
	}

	json_t* pDbObject = json_object_get(pRoot, IMA_JSON_KEY_DB);
	if(pDbObject == NULL) {
		LOG(error, __FILE__, __LINE__, "DB related info not present in the conf file");
		json_decref(pRoot);
		return -1;
	} else {
		if(json_is_object(pDbObject) == false) {
			LOG(error, __FILE__, __LINE__, "DB related info not in correct format");
			json_decref(pRoot);
			return -1;
		} else {
			if(readDbInfo(pDbObject) != 0) {
				LOG(error, __FILE__, __LINE__, "Failed to read DB info from conf file");
				json_decref(pRoot);
				return -1;
			}
		}
	}

	json_t* pSmtpObject = json_object_get(pRoot, IMA_JSON_KEY_SMTPSERVER);
	if(pSmtpObject == NULL) {
		LOG(error, __FILE__, __LINE__, "SMTP related info not present in the conf file");
		json_decref(pRoot);
		return -1;
	} else {
		if(json_is_object(pSmtpObject) == false) {
			LOG(error, __FILE__, __LINE__, "SMTP related info not in correct format");
			json_decref(pRoot);
			return -1;
		} else {
			if(readSmtpInfo(pSmtpObject) != 0) {
				LOG(error, __FILE__, __LINE__, "Failed to read SMTP info from conf file");
				json_decref(pRoot);
				return -1;
			}
		}
	}

	json_t* pMqObject = json_object_get(pRoot, IMA_JSON_KEY_MESSAGE_QUEUE);
	if(pMqObject == NULL) {
		LOG(error, __FILE__, __LINE__, "Message Queue related info not present in the conf file");
		json_decref(pRoot);
		return -1;
	} else {
		if(json_is_object(pMqObject) == false) {
			LOG(error, __FILE__, __LINE__, "Message queue related info not in correct format");
			json_decref(pRoot);
			return -1;
		} else {
			if(readMqInfo(pMqObject) != 0) {
				LOG(error, __FILE__, __LINE__, "Failed to read MQ info from conf file");
				json_decref(pRoot);
				return -1;
			}
		}
	}
	
	json_decref(pRoot);
	return 0;
}

int ImaConf::readDbInfo(json_t* pDbRoot) {
	LOG(debug, __FILE__, __LINE__, "ImaConf::readDbInfo called");

	if(pDbRoot == NULL) {
		LOG(error, __FILE__, __LINE__, "pDbRoot is NULL");
		return -1;
	}

	json_t* pDbHostObj = json_object_get(pDbRoot, IMA_JSON_KEY_HOSTNAME);
	if(pDbHostObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "DB hostname doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_string(pDbHostObj)) {
			strcpy(mDbHostname, json_string_value(pDbHostObj));
		} else {
			LOG(error, __FILE__, __LINE__, "DB hostname should be a string value");
			return -1;
		}
	}

	json_t* pDbPortObj = json_object_get(pDbRoot, IMA_JSON_KEY_PORT);
	if(pDbPortObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "DB port doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_integer(pDbPortObj)) {
			mDbPort = json_integer_value(pDbPortObj);
		} else {
			LOG(error, __FILE__, __LINE__, "DB port should be a integer value");
			return -1;
		}
	}

	json_t* pDbNameObj = json_object_get(pDbRoot, IMA_JSON_KEY_NAME);
	if(pDbNameObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "DB Name doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_string(pDbNameObj)) {
			strcpy(mDbName, json_string_value(pDbNameObj));
		} else {
			LOG(error, __FILE__, __LINE__, "DB name should be a string value");
			return -1;
		}
	}

	json_t* pDbUserObj = json_object_get(pDbRoot, IMA_JSON_KEY_USERNAME);
	if(pDbUserObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "DB username doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_string(pDbUserObj)) {
			strcpy(mDbUsername, json_string_value(pDbUserObj));
		} else {
			LOG(error, __FILE__, __LINE__, "DB username should be a string value");
			return -1;
		}
	}
			
	json_t* pDbPasswordObj = json_object_get(pDbRoot, IMA_JSON_KEY_PASSWORD);
	if(pDbPasswordObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "DB password doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_string(pDbPasswordObj)) {
			strcpy(mDbPassword, json_string_value(pDbPasswordObj));
		} else {
			LOG(error, __FILE__, __LINE__, "DB Password should be a string value");
			return -1;
		}
	}

	return 0;
}	

int ImaConf::readSmtpInfo(json_t* pSmtpRoot) {
	LOG(debug, __FILE__, __LINE__, "ImaConf::readSmtpInfo called");

	if(pSmtpRoot == NULL) {
		LOG(error, __FILE__, __LINE__, "pSmtpRoot is NULL");
		return -1;
	}

	json_t* pSmtpHostObj = json_object_get(pSmtpRoot, IMA_JSON_KEY_HOSTNAME);
	if(pSmtpHostObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "SMTP hostname doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_string(pSmtpHostObj)) {
			strcpy(mSmtpHost, json_string_value(pSmtpHostObj));
		} else {
			LOG(error, __FILE__, __LINE__, "SMTP hostname should be a string value");
			return -1;
		}
	}
	
	json_t* pSmtpPortObj = json_object_get(pSmtpRoot, IMA_JSON_KEY_PORT);
	if(pSmtpPortObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "SMTP port doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_integer(pSmtpPortObj)) {
			mSmtpPort = json_integer_value(pSmtpPortObj);
		} else {
			LOG(error, __FILE__, __LINE__, "SMTP port should be a integer value");
			return -1;
		}
	}

	json_t* pSmtpUserObj = json_object_get(pSmtpRoot, IMA_JSON_KEY_USERNAME);
	if(pSmtpUserObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "SMTP username doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_string(pSmtpUserObj)) {
			strcpy(mSmtpUsername, json_string_value(pSmtpUserObj));
		} else {
			LOG(error, __FILE__, __LINE__, "SMTP username should be a string value");
			return -1;
		}
	}
			
	json_t* pSmtpPasswordObj = json_object_get(pSmtpRoot, IMA_JSON_KEY_PASSWORD);
	if(pSmtpPasswordObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "SMTP password doesnt exist in the conf file");
		return -1;
	} else {
		if(json_is_string(pSmtpPasswordObj)) {
			strcpy(mSmtpPassword, json_string_value(pSmtpPasswordObj));
		} else {
			LOG(error, __FILE__, __LINE__, "SMTP Password should be a string value");
			return -1;
		}
	}

	return 0;
}


int ImaConf::readMqInfo(json_t* pMqRoot) {
	LOG(debug, __FILE__, __LINE__, "ImaConf::readMqInfo called");

	if(pMqRoot == NULL) {
		LOG(error, __FILE__, __LINE__, "pMqRoot is NULL");
		return -1;
	}

	json_t* pSysVRegConfCodeMqKeyObj = json_object_get(pMqRoot, IMA_JSON_KEY_REGCONFCODEQKEY);
	if(pSysVRegConfCodeMqKeyObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "Registration confirmation code Message queue key doesn't exist in the conf file");
		return -1;
	} else {
		if(json_is_integer(pSysVRegConfCodeMqKeyObj)) {
			mSysVRegConfCodeMsgQKey = json_integer_value(pSysVRegConfCodeMqKeyObj);
		} else {
			LOG(error, __FILE__, __LINE__, "Registration conf code Message queue key should be an integer value");
			return -1;
		}
	}

	json_t* pSysVNewPasswordMqKeyObj = json_object_get(pMqRoot, IMA_JSON_KEY_NEWPASSWORDQKEY);
	if(pSysVNewPasswordMqKeyObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "New password Message queue key doesn't exist in the conf file");
		return -1;
	} else {
		if(json_is_integer(pSysVNewPasswordMqKeyObj)) {
			mSysVNewPasswordQKey = json_integer_value(pSysVNewPasswordMqKeyObj);
		} else {
			LOG(error, __FILE__, __LINE__, "New password Message queue key should be an integer value");
			return -1;
		}
	}


	json_t* pSysVUserFeedbackMqKeyObj = json_object_get(pMqRoot, IMA_JSON_KEY_USERFEEDBACKQKEY);
	if(pSysVUserFeedbackMqKeyObj == NULL ) {
		LOG(error, __FILE__, __LINE__, "User feedback Message queue key doesn't exist in the conf file");
		return -1;
	} else {
		if(json_is_integer(pSysVUserFeedbackMqKeyObj)) {
			mSysVUserFeedbackQKey = json_integer_value(pSysVUserFeedbackMqKeyObj);
		} else {
			LOG(error, __FILE__, __LINE__, "User feedback Message queue key should be an integer value");
			return -1;
		}
	}


	return 0;
}

